<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb class="navigation">
      <el-breadcrumb-item :to="{ path: '/mapu' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>区域管理</el-breadcrumb-item>
      <el-breadcrumb-item>行政区管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体区域 -->
    <!-- 行政区管理 -->
    <el-card class="notice">
      <h4>&nbsp;&nbsp;行政区管理</h4>
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入行政区姓名" v-model="queryInfo.areaname" clearable @clear="getTypeList">
            <el-button slot="append" icon="el-icon-search" @click="getTypeList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">新增行政区</el-button>
        </el-col>
      </el-row>
      <!--行政区类型列表区-->
      <el-table :data="typelist" border stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column label="行政区编码" prop="regionCode"> </el-table-column>
        <el-table-column label="行政区名称" prop="regionName"> </el-table-column>
        <el-table-column label="行政区状态" prop="regionStatus" min-width="50px">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="typelistChanged(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="270px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeregionId(scope.row.regionCode)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 新增行政区 -->
    <el-dialog title="新增行政区" :visible.sync="addDialogVisible" width="30%" label-width="auto">
      <span>
        <el-form :model="addInfoForm" label-width="auto" ref="addFormRef" :rules="addFormRules">
          <el-form-item label="所属市:">
            <el-input v-model="addInfoForm.cityName" disabled type="text" style="width: 75%"></el-input>
          </el-form-item>
          <el-form-item label="行政区名称:" prop="regionName">
            <el-input v-model="addInfoForm.regionName" type="text" style="width: 75%"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button :plain="true" @click="open('addFormRef')">取 消</el-button>
        <el-button type="primary" @click="editInfoFormInfo('addFormRef')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'District',
  data () {
    return {
      // 获取用户列表的参数
      queryInfo: {
        areaname: '',
        // 当前页码
        pageNum: 1,
        // 当前每页显示多少条数据
        pageSize: 5
      },
      typelist: [],
      total: 0,
      // 控制增加行政区对话框的关闭
      addDialogVisible: false,
      // 增加行政区列表
      addInfoForm: {
        cityName: '成都市',
        regionName: ''
      },
      addFormRules: {
        regionName: [
          { required: true, message: '请输入行政区名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 获取行政区类型
    this.getTypeList()
  },
  methods: {
    getTypeList () {
      this.$axios.get('pogregion/search_level3', {
        params: this.queryInfo
      }).then(res => {
        const info = res.data
        if (res.data.code !== 200) {
          this.$message({
            message: '获取数据失败',
            type: 'error',
            offset: 70
          })
        }
        this.typelist = info.data.list
        this.total = info.data.total
      }).catch(err => {
        console.log(err)
      })
    },
    // 监听pagesize改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getTypeList()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getTypeList()
    },
    // 控制对话框关闭消息发送
    open (addFormRef) {
      this.$message('已取消增加')
      this.$refs[addFormRef].resetFields()
      this.addDialogVisible = false
    },
    async typelistChanged (row) {
      const { data: res } = await this.$http.put('manager/system/updateManagerStatus', {
        id: row.id,
        status: row.status
      })
      if (res.code === 500) {
        this.$message.error('更新管理员状态失败！')
      } else if (res.code === 402) {
        this.$message.error('无操作权限！')
      } else { this.$message.success('更新管理员状态成功！') }
      this.getTypeList()
    },
    async removeregionId (regionCode) {
      // 弹框询问
      const confirmResult = await this.$confirm(
        '此操作将永久删除该行政区类型, 是否继续?', '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => {
        return err
      })
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$axios.delete('pogregion/deletebyregionid/' + regionCode)
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功！')
      this.getTypeList()
    },
    // 新增行政区并提交
    editInfoFormInfo (addFormRef) {
      this.$refs[addFormRef].validate((valid) => {
        if (valid) {
          let param = {
            cityName: this.addInfoForm.cityName,
            regionName: this.addInfoForm.regionName
          }
          this.$axios({
            url: 'pogregion/region',
            method: 'post',
            data: param
          }).then(res => {
            if (res.data.code === 200) {
              this.$message({
                message: '新增成功',
                type: 'success',
                offset: 70
              })
              this.getTypeList()
            }
          })
            .catch((err) => {
              console.log(err)
            })
          // 关闭对话框
          this.addDialogVisible = false
          this.$refs[addFormRef].resetFields()
        }
      })
    }
  }
}
</script>

<style scoped>
.navigation {
  padding: 10px;
}

.notice {
  margin-bottom: 20px;
  border-radius: 15px;
  box-shadow: 15px 15px 10px -12px #4c61a0;
}

h4 {
  padding-bottom: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid #4c61a0;
}

.el-table {
  margin-top: 15px;
}
</style>
